package tpase.client.Application;

import it.unisa.dia.gas.jpbc.Pairing;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Pair;
import tpase.client.Handler.ClientSetupHandler;
import tpase.client.VO.Address;
import tpase.util.Util;

import java.io.IOException;

public class MainApplication extends Application {
    private Stage stage;
    private double width = 320.0;
    private double height = 280.0;
    private String clientPath = "src/main/resources/config";
    private String jpbcParamPath;
    private Address setupAddress;
    private Address clientAddress;
    private Pairing pairing;
    @Override
    public void start(Stage stage) throws Exception {
        this.stage = stage;
//        登录界面
        setLoginScene();
//        注册界面
        setRegisterScene();
//        主界面
        setMainScene();

        getPublicParameter();
    }

    public void loadParameters() {
        String jpbcParamName = "/a.properties";
        jpbcParamPath = clientPath + jpbcParamName;
        pairing = Util.getPairing(jpbcParamPath);
    }

    /**
     * 向 全局服务器 请求 获取公开参数（t, n 和 公开承诺值）
     */
    private void getPublicParameter() {
        String setupName = "/setupParameters.json";
        String clientName = "/clientParameters.json";
        setupAddress = Util.jsonFileToObj(clientPath + setupName, Address.class);
        clientAddress = Util.jsonFileToObj(clientPath + clientName, Address.class);
        Util.processClientBootstrap(new ClientSetupHandler(clientPath), clientAddress, setupAddress);
        loadParameters();
    }

    private void setLoginScene() throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(MainApplication.class.getResource("/tpase/client/login-view.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), width, height);
        //stage.setTitle("Login");
        stage.setScene(scene);
        stage.setResizable(true);
        stage.initStyle(StageStyle.TRANSPARENT);

        stage.show();
    }

    private void setRegisterScene() {

    }

    private void setMainScene() {

    }


    public static void main(String[] args) {
        launch();
    }
}
